<?php

/**
 * this class provides some useful functions for working with header columns of a data grid
 * @author		ungerboeck simon <su@militaerversand.at>
 *
 */
class DataColumn {
    //private $title = null;
    private $field = null;
    private $name = null;
    private $width = null;
    private $align = null;
    private $hidden = null;
    private $resizable = null;
    private $sortable = null;
    private $order = null;
    private $cellStyleFunc = null;
    private $checkbox = null;
    private $editor = null;



    /************************************************************************
     ************************** PUBLIC FUNCTIONS ****************************
     ************************************************************************/

    /**
     * sets the id and the name of the column
     * @param        string        $id
     * @param        string        $name
     */
    public function __construct($id, $name = ' ') {
        if ($id != null and $name != null) {
            $this->field = $id;
            $this->name = $name;
            $this->align = 'left';
            $this->hidden = false;
            $this->resizeable = true;
            $this->sortable = true;
            $this->setAlignLeft();
            $this->checkbox = false;
        } else
            throw new Exception('id or name if data column is null!');
    }


    /**
     * creates the data column (header column of grid)
     * @return         string        $output
     */
    public function create() {
        $output = '<th data-options="';
        $output .= (isset($this->field) ? 'field:\'' . $this->field . '\', ' : '');
        $output .= (isset($this->checkbox) ? 'checkbox:' . (($this->checkbox) ? 'true,' : 'false,') : '');
        $output .= (isset($this->editor) ? 'editor:\'' . $this->editor . '\',' : '');
        $output .= (isset($this->cellStyleFunc) ? 'styler:' . $this->cellStyleFunc . ', ' : '');
        $output .= (isset($this->width) ? 'width:' . $this->width . ', ' : '');
        $output .= (isset($this->align) ? 'align:\'' . $this->align . '\',' : '');
        $output .= (isset($this->hidden) ? 'hidden:' . (($this->hidden) ? 'true,' : 'false,') : '');
        $output .= (isset($this->resizable) ? 'resizable:' . (($this->resizable) ? 'true,' : 'false,') : '');
        $output .= (isset($this->sortable) ? 'sortable:' . (($this->sortable) ? 'true,' : 'false,') : '');
        $output .= (isset($this->order) ? 'order:\'' . $this->order . '\'' : '');
        $output .= '"><span class="header">' . $this->name . '</span></th>';
        return $output;
    }
    
    
    /**
     * hides the column
     */
    public function hide(){
        $this->hidden = true;
    }
    
    
    /**
     * shows the column
     */
    public function show(){
        $this->hidden = false;
    }

    
    /**
     * enables checkboxes for the grid
     */
    public function enableCheckboxes(){
    	$this->checkbox = true;
    }
    
    
    public function enableNumericEditing(){
    	$this->editor = 'numberbox';
    }

    /************************************************************************
     **************************** GETTER / SETTER ***************************
     ************************************************************************/

    public function setId($id) {
        $this->field = $id;
    }


    public function setWidth($width) {
        if (is_numeric($width))
            $this->width = $width;
        else
            throw new Exception('your given width for data column is not a numeric value');
    }


    public function setAlignLeft() {
        $this->align = 'left';
    }


    public function setAlignRight() {
        $this->align = 'right';
    }
    
    
    public function setResizable($resizable){
        $this->resizable = $resizable;
    }
    
    
    public function setSortable($sortable){
    	$this->sortable = $sortable;
    }
    
    
    public function setOrder($order){
    	$this->order = $order;
    }
    
    public function setCellStyleFunc($function){
    	$this->cellStyleFunc = $function;
    }
    

    public function getId() {
        return $this->field;
    }


    public function getWidth() {
        return $this->width;
    }


    public function isAlignLeft() {
        return ($this->align == 'left' ? true : false);
    }
    
    
    public function isResizable(){
        return $this->resizable;
    }
    
    
    public function getSortable(){
    	return $this->sortable;
    }
    
    
    public function getOrder(){
    	return $this->order;
    }


    /************************************************************************
     ************************** PRIVATE FUNCTIONS ***************************
     ************************************************************************/
}

?>